package com.yiren.dbaa.module.practice_set.line.presenter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.line.contract.LineHistoryContract;
import com.yiren.dbaa.module.practice_set.line.contract.LineHomeContract;
import com.yiren.dbaa.module.practice_set.line.contract.LineReleaseContract;
import com.yiren.dbaa.module.practice_set.line.model.LineHistoryModel;
import com.yiren.dbaa.module.practice_set.line.model.LineHomeModel;
import com.yiren.dbaa.module.practice_set.line.model.LineReleaseModel;
import com.yiren.dbaa.module.practice_set.line.view.activity.LineEditActivity;
import com.yiren.dbaa.module.practice_set.site.bean.LineDataValBean;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/16 17:45
 * @description: 线路历史版本
 */
public class LineHistoryPresenter implements LineHistoryContract.Presenter {

    private Activity activity;
    private LineHistoryContract.Model versionHistoryModel;
    private LineHomeContract.Model lineHomeModel;
    private UserParamConfigBean userParamConfigBean;
    private LineReleaseContract.Model releaseModel;

    public LineHistoryPresenter(Activity activity) {
        this.activity = activity;
        versionHistoryModel = new LineHistoryModel(activity);
        lineHomeModel = new LineHomeModel(activity);
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
        releaseModel = new LineReleaseModel(activity);
    }

    @Override
    public void loadVersionList(LineHistoryContract.View view, String userId, LineInfoEntity lineInfo) {
        LoadDialog.show(activity, "正在加载数据...");
        versionHistoryModel.getVersionList(userId, lineInfo.getSiteCode(), lineInfo.getName(), new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<LineInfoEntity> lineList = (List<LineInfoEntity>) result;
                view.showVersionList(lineList);
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void downLoadLineData(LineInfoEntity lineInfo, LineHistoryContract.View view) {
        activity.runOnUiThread(() -> LoadDialog.show(activity, "正在下载中..."));
        int pUserID;
        int cUserID;
        if (userParamConfigBean.isPartner()) {
            pUserID = userParamConfigBean.getUserInfo().getId();
            cUserID = userParamConfigBean.getUserInfo().getId();
        } else {
            pUserID = userParamConfigBean.getUserInfo().getpUserId();
            cUserID = userParamConfigBean.getUserInfo().getId();
        }
        lineHomeModel.downLoadLinePointData(lineInfo.getName(), lineInfo.getLineVer(), lineInfo.getSiteCode(), pUserID, cUserID, new CommonCallBack() {
            @Override
            public void success(Object result) {
                //本地数据插入
                LineDataValBean lineDataValBean = (LineDataValBean) result;
                lineInfo.setPartneModifyVal(lineDataValBean.getPartnerLineData());
                lineInfo.setCoachModifyVal(lineDataValBean.getCoachLineData());
                if (GradeDbController.getInstance(false).insertOrReplace(lineInfo)) {
                    //通知视图
                    LoadDialog.dismiss(activity);
                    MessageBox.MessageAndVoiceModel(activity, "下载成功");
                    CommonDialog.showTwoBtn(activity, "操作确认", "下载已完成，请问是直接发布，还是编辑线路？", "直接发布", "编辑线路", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (which == -2) {
                                releaseLine(lineInfo);
                            } else {
                                Intent intent = new Intent(activity, LineEditActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("line", lineInfo);
                                bundle.putInt("pageType", 2);
                                intent.putExtras(bundle);
                                activity.startActivity(intent);
                                activity.finish();
                            }
                            dialog.dismiss();
                        }
                    });
                } else {
                    MessageBox.MessageAndVoiceModel(activity, "下载失败");
                }

            }

            @Override
            public void fail(String error) {
                super.fail(error);
            }
        });
    }

    @Override
    public void releaseLine(LineInfoEntity lineInfo) {
        releaseModel.publishLine(lineInfo, userParamConfigBean.getUserInfo().getId().toString(), lineInfo.getUpdateDetails(), lineInfo.getUpdateMode(), new CommonCallBack() {
            @Override
            public void success(Object result) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        lineInfo.setLineVer(lineInfo.getLineVerLast() + 1);
                        GradeDbController.getInstance(false).update(lineInfo);
                        LoadDialog.dismiss(activity);
                        MessageBox.MessageAndVoiceModel(activity, "线路发布成功!");
                        activity.startActivity(new Intent(activity, PracticeCarSetActivity.class));
                    }
                });
            }

            @Override
            public void fail(String error) {
                activity.runOnUiThread(() -> CommonDialog.showOneBtn(activity, "操作提示", "线路数据发布失败!", "我知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }));
            }
        });

    }


}
